package com.study.firstline

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.ContextMenu
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import com.study.firstline.base.BaseActivity
import com.study.firstline.utils.ActivityCollector
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        test_btn1.setOnClickListener{
            Toast.makeText(this,"aaaaaaa",Toast.LENGTH_SHORT).show();
        }

        test_btn2.setOnClickListener{
            // 正常跳转
            /*val intent = Intent(this, SecondActivity::class.java);
            val data="hello";
            intent.putExtra("data",data);
            startActivity(intent);*/
            // 接收第二个activity返回值
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent,1);
        }

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu);
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // when用法
        when(item.itemId){
            R.id.add_item -> Toast.makeText(this,"新增",Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this,"删除",Toast.LENGTH_SHORT).show()
        }
        return true
    }

    /**
     * 获取返回值
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            1 -> if(resultCode== RESULT_OK){
                val stringExtra = data?.getStringExtra("data_return");
                Log.d("firstActivity","returned data is $stringExtra")
            }
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        ActivityCollector.finishAll();
        // 杀掉进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}